<script setup lang="ts">
import { useOrderStore } from '../../stores/order'
import {onMounted, ref} from 'vue'
import { useRouter } from 'vue-router'
import {ElMessage} from "element-plus";
const router = useRouter()
const store = useOrderStore()
const tableData = ref([])

onMounted(() => {
  store.fetchOrders()
})

// const getOrder = () => {
//   const query = {}
//   getOrders(query).then(res => {
//     tableData.value = res.data.records
//   })
// }
const getStatusText = (status) => {
  const statusMap = {
    0: '待支付',
    1: '已支付',
    2: '已取消',
    3: '已完成'
  }
  return statusMap[status] || '未知状态'
}

const getStatusType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'success',
    2: 'info',
    3: ''
  }
  return typeMap[status] || ''
}

const handleDetail = (id) => {
  router.push({ name: 'order-detail', params: { id } })
}

const handleCreate = () => {
  router.push({ path: '/order/create' })
  console.log('创建订单')
}

const handleCancel = async (id) => {
  try {
    await store.cancel(id)
    ElMessage.success('订单已取消')
  } catch (error) {
    ElMessage.error(error.message)
  }
}

const handlePayment = (orderId) => {
  router.push({ path: `/order/payment/${orderId}` })
}

const handlePageChange = () => {
  store.fetchOrders()
}
</script>

<template>
  <div class="order-list">
    <div class="header">
      <h2>订单列表</h2>
      <el-button type="primary" @click="handleCreate">新建订单</el-button>
    </div>

    <el-table :data="store.orders" v-loading="store.loading"  border style="width: 100%" stripe>
      <el-table-column prop="orderNo" label="订单编号" width="180" />
      <el-table-column prop="orderName" label="订单名称" />
      <el-table-column prop="price" label="金额" width="120">
        <template #default="{row}">
          ¥{{ row.price.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{row}">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="180">
        <template #default="{row}">
          <el-button size="small" @click="handleDetail(row.id)">详情</el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleCancel(row.id)"
              :disabled="row.status !== 0"
          >
            取消
          </el-button>
          <el-button
              size="small"
              type="success"
              @click="handlePayment(row.id)"
              :disabled="row.status !== 0"
          >
            支付
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-model:current-page="store.pagination.page"
        v-model:page-size="store.pagination.pageSize"
        :total="store.pagination.total"
        @current-change="handlePageChange"
        layout="total, sizes, prev, pager, next, jumper"
    />
  </div>
</template>

<style scoped >
.order-list {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>